package com.aolong.archives.common.resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import com.aolong.archives.common.util.Const;
import com.aolong.archives.core.sysmanage.entity.DataDictionaryEntity;
import com.rock.platform.util.SessionUtils;

public class DataDictionaryUtil {

	private static final DataDictionaryUtil _instance = new DataDictionaryUtil();    
	
	public static DataDictionaryUtil getInstance() {    
	    return _instance;    
	}    
	
	private DataDictionaryUtil()
	{
	}
	
	/**
	 * 加载数据字典资源
	 * @param em
	 */
	public void loadResource(EntityManager em)
	{
		@SuppressWarnings("unchecked")
		List<DataDictionaryEntity> listEntity = em.createQuery("from DataDictionaryEntity").getResultList();
        this.addResource(listEntity);
	}
	
	/**
	 * 增加数据字典资源
	 * @param listEntity
	 */
	public void addResource (List<DataDictionaryEntity> listEntity) 
	{
        if (listEntity == null) return;

        for(DataDictionaryEntity entity : listEntity)
		{
        	String category = entity.getCategory().toLowerCase();
			Map<String,DataDictionaryEntity> map = lookup.get(category);
			if (map == null)
			{
				map = new HashMap<String,DataDictionaryEntity>();
				lookup.put(category, map);
			}
			map.put(entity.getCode(),entity);
		}
    }
	
	/**
	 * 清空内存中数据字典资源
	 */
	public void clearResource()
	{
		lookup.clear();
	}
	
	private Map<String,DataDictionaryEntity> getLookup(String category)
	{
		Map<String,DataDictionaryEntity> _lookup = lookup.get(category);
		return (_lookup == null) ? new HashMap<String,DataDictionaryEntity>() : _lookup;
	}

	/**
	 * 根据分类、编号获取数据字典中国际化的value信息， 自动识别session中语言信息  
	 * @param code
	 * @param category
	 * @return
	 */
	public String getValue(Object code,String category) {
        
		return getValue(code,category,SessionUtils.getAttributeText(SessionUtils.SKEY_LANGUAGE));
	}
	
	/**
	 * 根据分类、编号、语言  获取数据字典中的国际化value信息
	 * @param code
	 * @param category
	 * @return
	 */
	public String getValue(Object code,String category,String language) {
        
		if (code == null) {
            return "";
        }
		
        DataDictionaryEntity entity = getLookup(category.toLowerCase()).get(code.toString());
        if (entity == null)
        {
        	return code.toString();
        }
        else
        {
        	return (language.equals(Const.LANGUAGE_en_US)) ? entity.getValueEn() : entity.getValue();
        }
	}
    
	/**
	 * 根据分类获取数据字典实体信息列表 
	 * @param category
	 * @return
	 */
	public List<DataDictionaryEntity> getDataDictionarysByCategory(String category)
	{
		Map<String,DataDictionaryEntity> _lookup = getLookup(category.toLowerCase());
		List<DataDictionaryEntity> valueList = new ArrayList<DataDictionaryEntity>(_lookup.values());
		return valueList;
	}
	
    private Map<String,Map<String,DataDictionaryEntity>> lookup = new HashMap<String,Map<String,DataDictionaryEntity>>() ;
}
